<?php
/**
 * Scheduler module for Joomla! 1.5
 * @author      DHTMLX, <dhtmlx.gmail.com>
 * @package		scheduler
 * @subpackage	scheduler
 * @version     1.0.0
 * @copyright   Copyright (C) 2009 DHTML LTD , http://dhtmlx.com
 * @license     http://www.gnu.org/licenses/gpl.html GNU/GPL
 */

defined ('_JEXEC') or die();
jimport( 'joomla.application.component.model' );

class SchedulerModelScheduler extends JModel
{
	var $scheduler_lang;
	var $lang_name;
	var $scheduler_log = false;
	var $scheduler_debug = false;

	function getLangs() {
		$document = & JFactory::getDocument();
		$locale = substr($document->language,0 ,2);
		$lang_path = JPATH_COMPONENT.DS.'sources'.DS.$locale.'.php';
		if (file_exists($lang_path)) {
			include($lang_path);
		} else {
			include(JPATH_COMPONENT.DS.'sources'.DS.'en.php');
		}
		$this->lang_name = $locale;
		$this->scheduler_lang = $scheduler_lang;
	}

	function getScheduler() {
		$config =& JFactory::getConfig();
		if ((isset($config->_registry['config']['data']->scheduler_log))&&($config->_registry['config']['data']->scheduler_log == true)) {
			$this->scheduler_log = true;
		} else {
			$this->scheduler_log = false;
		}
		$this->getLangs();
		$options = $this->getOptions();
		$usertype = $this->getUser();
		
		$options = $this->checkMode($options);
		if (!$options) {
			return $this->lang_replacer('[[_SCHEDULER_tabs_warning]]');
		}

		$scheduler = '
			<script src="'.JURI::root().'components/com_scheduler/codebase/dhtmlxscheduler.js" type="text/javascript" charset="utf-8"></script>
			<script src="'.JURI::root().'components/com_scheduler/codebase/sources/locale_'.$this->lang_name.'.js" type="text/javascript" charset="utf-8"></script>
			<link rel="stylesheet" href="'.JURI::root().'components/com_scheduler/codebase/dhtmlxscheduler.css" type="text/css" title="no title" charset="utf-8">';
		
		if ($options['year'] == '1') {
			$scheduler .= '
			<script src="'.JURI::root().'components/com_scheduler/codebase/ext/dhtmlxscheduler_year_view.js" type="text/javascript" charset="utf-8"></script>';
		}
		if ($options['agenda'] == '1') {
			$scheduler .= '
			<script src="'.JURI::root().'components/com_scheduler/codebase/ext/dhtmlxscheduler_agenda_view.js" type="text/javascript" charset="utf-8"></script>';
		}

		if ($options['repeat'] == '1') {
			$scheduler .= '
			<script src="'.JURI::root().'components/com_scheduler/codebase/dhtmlxscheduler_recurring.js" type="text/javascript" charset="utf-8"></script>
			<script src="'.JURI::root().'components/com_scheduler/codebase/sources/locale_recurring_'.$this->lang_name.'.js" type="text/javascript" charset="utf-8"></script>
			<link rel="stylesheet" href="'.JURI::root().'components/com_scheduler/codebase/dhtmlxscheduler_recurring.css" type="text/css" title="no title" charset="utf-8">';
		}

		$scheduler .= '
			<script type="text/javascript" charset="utf-8">
			window.onload =	function init() {
					scheduler.config.xml_date="%Y-%m-%d %H:%i";
					scheduler.config.details_on_create=true;
					scheduler.config.details_on_dblclick=true;';
		if ($options['first_day'] == 'sunday') {
			$scheduler .= 'scheduler.config.start_on_monday=false;';
		}
		
		if ($options['12hr'] == '1') {
			$scheduler .= 'scheduler.config.hour_date="%h:%i %A";';
		}
		$scheduler .= '
					scheduler.init("scheduler_here",null,"'.$options['default_mode'].'");
					scheduler.load("'.JURI::root().'components/com_scheduler/events_rec.php?uid="+scheduler.uid());
					var dp = new dataProcessor("'.JURI::root().'components/com_scheduler/events_rec.php");';

		if ($options[$usertype] == '0') {
			$scheduler .= 'scheduler.config.readonly_form = true;
									scheduler.config.drag_create = false;
									scheduler.config.dblclick_create = false;
									scheduler.attachEvent(\'onClick\',function(id){ scheduler.showLightbox(id); return false; });
									scheduler.attachEvent(\'onBeforeDrag\',function(){return false;});';
		}
		$scheduler .= "
					dp.init(scheduler);
				}\n";
				
		if ($options['debug'] == '1') {
			$scheduler .= 'dhtmlxError.catchError("LoadXML",function(a,b,c){
				var html = "The text below, contains details about of server side problem.<hr><pre style=\"font_size: 8pt;\">"+ c[0].responseText;
				document.body.style.backgroundColor = "#dddddd";
				document.body.innerHTML = html;
				})';
		}
		
		$scheduler .= '</script>
			<div id="scheduler_here" class="dhx_cal_container" style="width:100%; height:600px; position: relative;">
				<div class="dhx_cal_navline">
					<div class="dhx_cal_prev_button">&nbsp;</div>
					<div class="dhx_cal_next_button">&nbsp;</div>
					<div class="dhx_cal_today_button"></div>
					<div class="dhx_cal_date"></div>';

		$tabs_count = 0;
		$tabsOffset = 20;
		$tabs = Array('day', 'week', 'month', 'year', 'agenda');
		for ($i = 0; $i < count($tabs); $i++) {
			if ($options[$tabs[$i]] == '1') {
				$tabs_count++;
			}
		}
		
		if ($options['day'] == '1') {
			$tabs_count--;
			$scheduler .= '
						<div class="dhx_cal_tab" name="day_tab" style="right:'.($tabsOffset + $tabs_count*64).'px;"></div>';
		}
		if ($options['week'] == '1') {
			$tabs_count--;
			$scheduler .= '
						<div class="dhx_cal_tab" name="week_tab" style="right:'.($tabsOffset + $tabs_count*64).'px;"></div>';
		}
		if ($options['month'] == '1') {
			$tabs_count--;
			$scheduler .= '
						<div class="dhx_cal_tab" name="month_tab" style="right:'.($tabsOffset + $tabs_count*64).'px;"></div>';
		}
		if ($options['year'] == '1') {
			$tabs_count--;
			$scheduler .= '
						<div class="dhx_cal_tab" name="year_tab" style="right:'.($tabsOffset + $tabs_count*64).'px;"></div>';
		}
		if ($options['agenda'] == '1') {
			$tabs_count--;
			$scheduler .= '
						<div class="dhx_cal_tab" name="agenda_tab" style="right:'.($tabsOffset + $tabs_count*64).'px;"></div>';
		}
		$scheduler .= '
					</div>
				<div class="dhx_cal_header"></div>
				<div class="dhx_cal_data"></div>
				<div style="position:absolute; bottom:5px; right:20px; font: Tahoma 8pt; color:black;">Powered by <a href="http://dhtmlx.com" target="_blank" style="color:blue; font-weight:normal;">dhtmlxScheduler</a></div>
			</div>' ;
		
		$final = $this->lang_replacer($scheduler);
		return $final;
		}
	
	
	function getOptions() {
		$query = 'SELECT `name`, `value` FROM #__scheduler_options ORDER BY `order`';		
		
		$this->_db->setQuery($query);
		$options_from_db = $this->_db->loadObjectList();
		for ($i = 0; $i < count($options_from_db); $i++) {
			$options[$options_from_db[$i]->name] = $options_from_db[$i]->value;
			}
		
		if ($this->scheduler_log == true)
			error_log("SchedulerModelScheduler->getOptions() was called\r\nresult = ".print_r($options, true)."\r\n\r\n", 3, JPATH_ROOT."/scheduler.log");
		
		return $options;
		}
	
	
	function lang_replacer($str) {
		$final = preg_replace_callback("/\[\[(_SCHEDULER_.*)\]\]/U", array($this, 'lang_callback'), $str);
		return $final;
		}
	
	
	function lang_callback($matches) {
		if (isset($this->scheduler_lang[$matches[1]])) {
			return $this->scheduler_lang[$matches[1]];
			}
			else {
			return $matches[0];
			}
		}
	
	
	function getUser() {
		$user =& JFactory::getUser();
		switch($user->usertype) {
			case '':
				$usertype = 'guests';
				break;
			case 'Super Administrator':
				$usertype = 'super_administrator';
				break;
			default:
				$usertype = strtolower($user->usertype);
			}
		if ($this->scheduler_log == true)
			error_log("SchedulerModelScheduler->getUser() was called\r\nresult = ".$usertype."\r\n\r\n", 3, JPATH_ROOT."/scheduler.log");
		return $usertype;
		}
	
	
	function checkMode($options) {
		$modes = Array('day', 'week', 'month', 'year', 'agenda');
		$mod = false;
		if ($options[$options['default_mode']] == '0') {
			for ($i = 0; $i < count($modes); $i++) {
				if ($options[$modes[$i]] == '1') {
					$mod = true;
					$options['default_mode'] = $modes[$i];
					break;
				}
			}
		} else {
			$mod = true;
		}

		if ($mod == false) {
			return false;
		} else {
			return $options;
		}
	}

}